package utils

import (
	"fmt"
	"log"
	"push_course/src/data"
	"push_course/src/entity"
	"strings"
	"sync"
)

var ins *Pusher
var lock sync.Mutex

type Pusher struct {}

func (this *Pusher)PushMsgToWechat(title,content string)  {
	res:=entity.Res{}
	PushMsgToWechatRpc(entity.Req{
		Title:   title,
		Content: content,
	},&res)
	log.Println("PushToWechatRpc:",res.Msg)
}




// 群组推送
func (this *Pusher) PushMsgToWechatGroup(title string, content string, group string) {
	res:=entity.Res{}
	// 调用 rpc
	PushMsgToWechatGroupRpc(entity.Req{
		Title:   title,
		Content: content,
		Group:   group,
	},&res)
	log.Println("PushToWechatGroupRpc:",res.Msg)
}





// 推送上午课表
func (this *Pusher) PushInMornCourseFF(weekDay int) {
	courseList := CourseList_ff
	this.PushMsgToWechatGroup("上午课表", fmt.Sprintf("<h3>8:20 %v</h3>\n<h3>10:20 %v</h3> %s", courseList[weekDay].A, courseList[weekDay].B, `<img src="https://lzfblog.cn/upload/2021/09/image-b64d1143497d4515a9cebf067e33e700.png">`), data.GROUP_COURSE)
}

// 推送下午课表
func (this *Pusher) PushInAfternoonCourseFF(weekDay int) {
	courseList := CourseList_ff
	this.PushMsgToWechatGroup("下午课表", fmt.Sprintf("<h3>14:00 %v</h3>\n<h3>16:00 %v</h3> %s", courseList[weekDay].C, courseList[weekDay].D, `<img src="https://lzfblog.cn/upload/2021/09/image-b64d1143497d4515a9cebf067e33e700.png">`), data.GROUP_COURSE)
}

// 推送晚上课表
func (this *Pusher) PushInEvenCourseFF(weekDay int) {
	courseList := CourseList_ff
	this.PushMsgToWechatGroup("晚上课表", fmt.Sprintf("<h3>19:00 %v</h3> %s", courseList[weekDay].E, `<img src="https://lzfblog.cn/upload/2021/09/image-b64d1143497d4515a9cebf067e33e700.png">`), data.GROUP_COURSE)
}

// ------------------------

// 推送上午课表
func (this *Pusher) PushInMornCourseYY(weekDay int) {
	courseList := CourseList_yy
	this.PushMsgToWechatGroup("上午课表", fmt.Sprintf("<h3>8:00 %v</h3>\n<h3>10:00 %v</h3> %s", courseList[weekDay].A, courseList[weekDay].B, `<img src="https://lzfblog.cn/upload/2021/09/image-acace424274942da81f14b377f21dbae.png">`), data.GROUP_STU)
}

// 推送下午课表
func (this *Pusher) PushInAfternoonCourseYY(weekDay int) {
	courseList := CourseList_yy
	this.PushMsgToWechatGroup("下午课表", fmt.Sprintf("<h3>13:30 %v</h3>\n<h3>15:45 %v</h3> %s", courseList[weekDay].C, courseList[weekDay].D, `<img src="https://lzfblog.cn/upload/2021/09/image-acace424274942da81f14b377f21dbae.png">`), data.GROUP_STU)
}

// 推送晚上课表
func (this *Pusher) PushInEvenCourseYY(weekDay int) {
	courseList := CourseList_yy
	this.PushMsgToWechatGroup("晚上课表", fmt.Sprintf("<h3>19:00 %v</h3> %s", courseList[weekDay].E, `<img src="https://lzfblog.cn/upload/2021/09/image-acace424274942da81f14b377f21dbae.png">`), data.GROUP_STU)
}

//每日一问：今天你学习了吗？<
func (this *Pusher) EveyDayTips(title, context string) {
	this.PushMsgToWechatGroup(title, "<h3>"+context+"</h3>", data.GROUP_STU)
}

// 推送天气
func (this *Pusher) PushWeather(title, city string) {
	weather, cold := GetCityWeather(city)
	this.PushMsgToWechatGroup(title, fmt.Sprintf("<p>%v</p> <strong>%v</strong>", weather, cold), data.GROUP_STU)
}

// 随机推送单词
func (this *Pusher) PushRandomWord(title string, n int) {
	hashMap := make(map[string]string)
	builder := strings.Builder{}
	for i := 0; i < n; i++ {
		en, zh := GetRandomWord()
		if _, ok := hashMap[zh]; ok {
			continue
		}
		builder.WriteString(fmt.Sprintf("%v   %v", en, zh))
	}

	this.PushMsgToWechatGroup(title, builder.String(), data.GROUP_STU)
}


// singleton
func GetIns() *Pusher {
	lock.Lock()
	defer lock.Unlock()
	if ins == nil {
		ins = &Pusher{}
	}
	return ins
}
